package com.ruoyi.notice.service.impl;

import java.util.List;
import java.util.Optional;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.notice.domain.Content;
import com.ruoyi.notice.mapper.ContentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.notice.mapper.NoticeMapper;
import com.ruoyi.notice.domain.Notice;
import com.ruoyi.notice.service.INoticeService;

/**
 * 公告Service业务层处理
 * 
 * @author li
 * @date 2025-05-18
 */
@Service
public class NoticeServiceImpl implements INoticeService 
{
    @Autowired
    private NoticeMapper noticeMapper;
    @Autowired
    private ContentMapper ContentMapper;

    /**
     * 查询公告
     * 
     * @param id 公告主键
     * @return 公告
     */
    @Override
    public Notice selectNoticeById(Long id)
    {
//        用mysql查询除公告外的所有信息
        Notice notice = noticeMapper.selectNoticeById(id);
//        用mongodb查询公告数据并赋值给notice
        Optional<Content> byId = ContentMapper.findById(id);
        Content noticeContent = byId.orElse(new Content());
        notice.setNotice(noticeContent.getContent());

        return notice;
    }

    /**
     * 查询公告列表
     * 
     * @param notice 公告
     * @return 公告
     */
    @Override
    public List<Notice> selectNoticeList(Notice notice)
    {
        List<Notice> notices = noticeMapper.selectNoticeList(notice);
        for (Notice notice1 : notices) {
            Long id = notice1.getId();
//            Content content = ContentMapper.findById(id.toString()).orElse(new Content());
            Optional<Content> byId = ContentMapper.findById(id);
            Content content = byId.orElse(new Content());
//            System.err.println(content.getContent());
            notice1.setNotice(content.getContent());
        }
//        System.err.println(notices);
        return notices;
    }

    /**
     * 新增公告
     * 
     * @param notice 公告
     * @return 结果
     */
    @Override
    public int insertNotice(Notice notice)
    {
        notice.setCreateTime(DateUtils.getNowDate());
//        调用mysql进行存储数据 存储除了评论内容之外的所有信息
        int i = noticeMapper.insertNotice(notice);
        if(i>0){
            //        调用mongodb进行存储评论内容信息
            Content noticeContent = new Content();
            noticeContent.setId(notice.getId());
//            System.err.println(noticeContent.getId());
            noticeContent.setContent(notice.getNotice());
//            System.err.println(notice.getNotice());
            ContentMapper.insert(noticeContent);
        }

        return i;
    }

    /**
     * 修改公告
     * 
     * @param notice 公告
     * @return 结果
     */
    @Override
    public int updateNotice(Notice notice)
    {
        notice.setUpdateTime(DateUtils.getNowDate());
        int i = noticeMapper.updateNotice(notice);
//        System.err.println(i);
        if (i>0){
            Content content = new Content();
            content.setContent(notice.getNotice());
//            System.err.println(notice.getNotice());
            content.setId(notice.getId());
//            System.err.println(content.getId());
//            System.err.println(content.getContent());
            ContentMapper.save(content);
        }
        return i;
    }

    /**
     * 批量删除公告
     * 
     * @param ids 需要删除的公告主键
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] ids)
    {
        int i = noticeMapper.deleteNoticeByIds(ids);
        if (i>0){
            for (Long id : ids) {
                ContentMapper.deleteById(id);
            }
        }
        return i;
    }

    /**
     * 删除公告信息
     * 
     * @param id 公告主键
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long id)
    {
        int i = noticeMapper.deleteNoticeById(id);
        if (i>0){
            ContentMapper.deleteById(id);
        }
        return i;
    }

    @Override
    public List<Notice> getNowNoticeList() {
        List<Notice> nowNoticeList = noticeMapper.getNowNoticeList();
        for (Notice notice : nowNoticeList) {
            Long id = notice.getId();
            Optional<Content> content = ContentMapper.findById(id);
            Content content1 = content.orElse(new Content());
            notice.setNotice(content1.getContent());
        }
        return nowNoticeList;
    }
}
